import { dictionaryGroupType, dictionaryType, treeType } from '@/types/project';
import { getObjectType, hasProperties } from '@/utils/utils';
import { treeBus } from '@/views/bus/treeData';
import { message } from 'ant-design-vue';
import { storeToRefs } from 'pinia';
import { ref } from 'vue';
import { projectStore } from './../modules/project';

/**
 * todo:设置值，如果是数组则覆盖，如果是单个对象，则push
 *  */
export const setTreeData = (data: any) => {
    var store = storeToRefs(projectStore())
    var type = getObjectType(data)
    if (type === "array" && data instanceof Array) {
        store.treeData.value = data
    } else {
        var res = store.treeData.value.find(item => {
            if (item.id == data.id) {
                return item
            }
        })
        if (!res) {
            store.treeData.value.push(data as treeType)
            return
        }
        message.warn("这个表已经存在")
    }

}

export const getTreeData = () => {
    var store = storeToRefs(projectStore())
    return store.treeData.value
}
/* 删除自己 */
export const deleteSelf = (item: treeType) => {
    var store = storeToRefs(projectStore())
    const findtree = (arr: Array<any>, item: treeType) => {
        arr.forEach((e, index: number) => {
            if (e.id === item.id) {
                arr.splice(index, index + 1)
                return
            } else if (e.children.length > 0) {
                findtree(e.children, item)
            }
        })
    }
    findtree(store.treeData.value, item)
    treeBus.selectItem = {}
}
var target: any = null
export const findItem = (id: string) => {
    var store = storeToRefs(projectStore())
    const findtree = (arr: Array<any>, id: string) => {
        arr.forEach((e) => {
            if (e.id === id) {
                target = e
                return
            } else if (e.children.length > 0) {
                findtree(e.children, id)
            }
        })
    }
    findtree(store.treeData.value, id)
    return target
}

/**
 * todo:保存选中的数据到本地,只能是文件夹类型
 *  */
export const saveLocalData = () => {
    var data = treeBus.selectItem
    var isEmpty = hasProperties(data)
    if (data.type === 'folder' && isEmpty) {
        message.warn("保存成功")
        setTreeData(data)
    } else {
        message.warn("保存遇到问题")
    }
}

/**
 * todo:是否展开所有文件
 *  */
export const isExpandedAll = (isExpanded: boolean) => {
    var store = storeToRefs(projectStore())
    const findtree = (arr: Array<any>) => {
        arr.forEach((e) => {
            if (e.children.length > 0) {
                findtree(e.children)
            } else {
                e.expand = isExpanded
            }
        })
    }
    findtree(store.treeData.value)
}
/**
 * todo:置顶
 *  */
export const moveToTop = (item: treeType) => {
    var store = storeToRefs(projectStore())
    const findtree = (arr: Array<any>) => {
        arr.forEach((e, index) => {
            if (e.children.length > 0) {
                findtree(e.children)
            } else {
                if (e.id === item.id) {
                    let res = arr.splice(index, index + 1)
                    arr.unshift(res.at(0))
                }
            }
        })
    }
    findtree(store.treeData.value)
}
/**
 * todo:移动到底部
 *
 */
export const moveToBottom = (item: treeType) => {
    const findtree = (arr: Array<any>) => {
        arr.forEach((e, index) => {
            if (e.id === item.id) {
                let res = arr.splice(index, index + 1)
                arr.push(res[0])
                return
            }
            else if (e.children.length > 0) {
                findtree(e.children)
            }
        })
    }
    findtree(getTreeData())
}

/**
 * todo:向上移动一层
 *  */
export const moveUp = (item: treeType) => {
    var store = storeToRefs(projectStore())
    const findtree = (arr: Array<any>) => {
        arr.forEach((e, index) => {
            if (e.id === item.id && index > 0) {
                let res = arr.splice(index - 1, 1, item)
                arr.splice(index, 1, res.at(0))
            }
            else if (e.children.length > 0) {
                findtree(e.children)
            }
        })
    }
    findtree(store.treeData.value)
}

/**
 * todo:向下移动一层
 *  */
export const moveDown = (item: treeType) => {
    var store = storeToRefs(projectStore())
    const findtree = (arr: Array<any>) => {
        for (let index = arr.length - 1; index >= 0; index--) {
            const e = arr.at(index);
            if (e.id === item.id && index < arr.length - 1) {
                /* 删下一个 */
                let res = arr.splice(index + 1, 1, item)
                arr.splice(index, 1, res.at(0))
            }
            else if (e.children.length > 0) {
                findtree(e.children)
            }
        }
    }
    findtree(store.treeData.value)
}

/**
 * todo:设置激活项
 *  */
export const activeItem = (item: treeType | { [key: string]: any }) => {
    treeBus.selectItem = item
}

/**
 * todo:将属性格式化成表单形式
 * 标题作为一个数组["12"]
 * 数值作为一个数组["12"]
 *  */

export const formatToTable = (array: Array<treeType>) => {
    let headers = new Set
    let values: any = []
    const findtree = (arr: Array<any>) => {
        arr.forEach((e) => {
            headers.add(e.key)
            values.push(e.value)
            if (e.children.length > 0) {
                findtree(e.children)
            }
        })
    }
    findtree(array)
    return {
        headers: Array.from(headers),
        values
    }
}


/**
 * todo:精简属性,仅上传关键值，用来统计分数
 *  */
export const optimizeProperties = (item: treeType) => {
    const findtree = (item: treeType) => {
        let obj = {
            title: item.title,
            type: item.type,
            value: "",
            score: "",
            children: [] as any,
            correctValue: item.correctValue
        }

        item.children.forEach((e) => {

            let temp = {
                title: e.title,
                type: e.type,
                value: "",
                score: "",
                children: [] as any,
                correctValue: e.correctValue
            }
            obj.children.push(temp)
            if (e.children.length > 0) {
                findtree(e)
            }
        })
        return obj
    }
    return findtree(item)
}


